//
//  BaseViewController.swift
//  CandyDate
//
//  Created by zolobdz on 2017/9/26.
//  Copyright © 2017年 HeChao. All rights reserved.
//

import UIKit
import ObjectMapper

class BaseViewController: UIViewController,RouterProtocol {

    lazy var qiniuToken = ""
    var simpleClosure :CDSimpleClosure? = nil
    
    override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?) {
        super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
        navigationItem.backBarButtonItem = UIBarButtonItem(title: "", style: .plain, target: nil, action: nil)
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = UIColor.hexString(hexString: "#EEF1EE");
        // Do any additional setup after loading the view.
        navigationItem.backBarButtonItem = UIBarButtonItem(title: "", style: .plain, target: nil, action: nil)
    }
    
    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        UIApplication.shared.sendAction(#selector(resignFirstResponder), to: nil, from: nil, for: nil)
    }

    //MARK: override
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        CDUtility.log("===============\(String(describing: type(of: self.self)))==================")
    }
    
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        super.touchesBegan(touches, with: event)
        view.endEditing(true)
    }
    
    override var prefersStatusBarHidden: Bool
    {
        return false
    }
    
    //MARK: Public
    
    
    //MARK: protocol
    func handle(parameter: RouterParameter?) {
        
    }
    
    //MARK: private

    // 用户登录后需要获取qiniutoken的方法 读取本地qntoken，如果没有或者是过期了自动获取，自动获取弹hud。6小时获取一次
    func getUserQiniuToken() {
        if let userID = MyselfSingleton.shared.userInfo?.userID
        {
            if let modelJSON = UserDefaults.standard.object(forKey: CDUserDefaultKey.userQiNiuToken + userID) as? Dictionary<String,String>,!modelJSON.isEmpty
            {
//                if let tokenModel = QiNiuToken(JSON: modelJSON)
//                {
//                    if !tokenModel.token.isEmpty
//                    {
//                        let formatter = DateFormatter()
//                        formatter.dateFormat = "yyyy/MM/dd HH:mm:ss"
//                        let expireDate = formatter.date(from: tokenModel.expireDate) ?? Date() // 过期时间
//                        let nowDate = Date(timeIntervalSinceNow: 0) // 现在时间
//                        if expireDate.compare(nowDate) == .orderedDescending
//                        {
//                            qiniuToken = tokenModel.token
//                            return
//                        }
//                        CDUtility.log("qiniu token过期啦")
//                    }
//                }
            }
            requestQiniuToken()
        }
    }
    
    // 获取登陆后的qiniuToken
    private func requestQiniuToken() {
        
        guard let userID = MyselfSingleton.shared.userInfo?.userID,userID.count > 0 else {
            return
        }
        
//        CDNetWorkService.requestDataWithTarget(
//            target: CDRequestAPI.getQiNiuToken(ShowHudKey: true, userID: userID),
//            type: QiNiuToken.self,
//            successClosure: { [weak self] (model,code,msg) in
//                if let strongSelf = self{
//                    if code == 10
//                    {
//                        if var m = model as? QiNiuToken
//                        {
//                            strongSelf.qiniuToken = m.token
//                            //设置过期时间
//                            let nowDate = Date(timeIntervalSinceNow: 60*60*6)//6小时后过期
//                            let formatter = DateFormatter()
//                            formatter.dateFormat = "yyyy/MM/dd HH:mm:ss"
//                            let dateString = formatter.string(from: nowDate)
//                            m.expireDate = dateString
//                            CDUtility.log("设置update qiniu token time : \(dateString)")
//
//                            let json = m.toJSON()
//                            UserDefaults.standard.set(json, forKey: CDUserDefaultKey.userQiNiuToken+userID)
//                        }
//                    }
//                    else{
//                        strongSelf.makeToast(msg)
//                    }
//
//                }
//        }) { [weak self] (msg) in
//            if let strongSelf = self{
//                strongSelf.makeToast(msg+"获取失败")
//            }
//        }
    }
    
}
